<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      v-model:page="page"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu="scope">
        <el-button type="primary" text icon="Memo" @click.stop="handleExamine(scope.row)"
          >审核
        </el-button>
      </template>
    </avue-crud>
  </basic-container>

  <el-dialog title="审核" v-model="dialogVisible" width="800px" :before-close="handleClose">
    <div class="d_box">
      <div class="d_row">
        <p class="d_p">申请人：</p>
        {{ rowData.name }}
      </div>
      <div class="d_row">
        <p class="d_p">申请人：</p>
        {{ rowData.phone }}
      </div>
      <div class="d_row">
        <p class="d_p">申请组织：</p>
        {{ rowData.orgName }}
      </div>
      <div class="d_row">
        <p class="d_p">机构代码：</p>
        {{ rowData.orgCode }}
      </div>
      <div class="d_row">
        <p class="d_p">行政区划：</p>
        {{ rowData.adnm }}
      </div>
      <div class="d_row">
        <p class="d_p">申请时间：</p>
        {{ rowData.createTime }}
      </div>
      <div class="d_row">
        <p class="d_p">审核意见：</p>
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入审核意见"
          v-model="opinion"
        >
        </el-input>
      </div>
      <div class="foot_box">
        <el-button @click="handleSubmit(false)">不同意</el-button>
        <el-button @click="handleSubmit(true)" type="primary">同意</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { list, saveRelation } from '@/api/organizational/join';
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: 'auto',
        indexLabel: '序号',
        indexWidth: '70',
        calcHeight: 32,
        tip: false,
        searchShow: true,
        searchMenuSpan: 3,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        addBtn: false,
        header: false,
        delBtn: false,
        column: [
          {
            label: '申请人',
            prop: 'name',
            search: true,
            slot: true,
          },
          {
            label: '手机号',
            prop: 'phone',
          },
          {
            label: '申请组织',
            prop: 'orgName',
          },
          {
            label: '机构代码',
            prop: 'orgCode',
          },
          {
            label: '行政区划',
            prop: 'adnm',
          },
          {
            label: '申请时间',
            prop: 'createTime',
          },
        ],
      },
      data: [],
      dialogVisible: false,
      rowData: {},
      opinion: '',
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: true,
        editBtn: false,
      };
    },
  },
  methods: {
    handleClose() {
      this.opinion = '';
      this.dialogVisible = false;
    },
    async handleSubmit(flag) {
      if (!this.opinion) {
        this.$message({
          type: 'error',
          message: '请输入审核意见!',
        });
        return;
      }
      if (flag) {
        let data = { ...this.rowData, reason: this.opinion, status: 2 };
        let res = await saveRelation(data);
        if (res.data.success == true) {
          this.dialogVisible = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        }
      } else {
        let data = { ...this.rowData, reason: this.opinion, status: 3 };
        let res = await saveRelation(data);
        if (res.data.success == true) {
          this.dialogVisible = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        }
      }
    },
    handleExamine(row) {
      this.rowData = row;
      this.dialogVisible = true;
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      list(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style scoped lang="scss">
.d_box {
  padding: 0 20px;
  box-sizing: border-box;
  .d_row {
    display: flex;
    align-items: flex-start;
    margin: 18px 0;
    .d_p {
      width: 100px;
      margin: 0;
    }
    .d_cloum {
      display: block;
      flex-direction: column;
      a {
        display: block;
        margin-bottom: 8px;
        color: #2196f3;
      }
    }
  }
  .foot_box {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin: 20px 0;
  }
}
</style>
